package com.play.ballen.mall.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.yuven.appframework.widget.SimpleTextWatcher

class MallBuyDialogUtils(private val mContext: Context) {

    val dialog: Dialog = Dialog(mContext)
    private lateinit var contentView: View
    private lateinit var edNum: EditText
    private lateinit var tvPrice: TextView

    private val mInputMethodManager by lazy {
        mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    }

    var numberCallback: ((Int) -> Unit)? = null

    private var priceSku: Float = 0f
    private var rate: Float = 0f    //汇率

    fun show(price: Float, rateValue: Float) {
        this.priceSku = price
        this.rate = rateValue
        if (!this::contentView.isInitialized) {
            contentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_mall_buy, null)
            contentView.let {
                edNum = it.findViewById(R.id.edNum)
                tvPrice = it.findViewById(R.id.tvPrice)

                it.findViewById<ImageView>(R.id.imgClose).setOnClickListener { btn ->
                    dialog.dismiss()
                }
                it.findViewById<TextView>(R.id.btnOk).setOnClickListener { btn ->
                    if (edNum.text.toString().isEmpty()) {
                        ToastUtils.show(edNum.hint.toString())
                        return@setOnClickListener
                    }
                    val num = edNum.text.toString().toInt()
                    if (num == 0) {
                        ToastUtils.show(edNum.hint.toString())
                        return@setOnClickListener
                    }
                    dialog.dismiss()
                    numberCallback?.invoke(num)
                }

                dialog.setContentView(it)
                dialog.setOnDismissListener { edNum.setText("") }
                dialog.setCanceledOnTouchOutside(false)
                dialog.window?.setLayout(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT
                )
                dialog.window?.setBackgroundDrawable(ColorDrawable())
                dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
                dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
            }

            /*edNum.filters = arrayOf(object : InputFilter {
                override fun filter(
                    source: CharSequence?,
                    start: Int,
                    end: Int,
                    dest: Spanned?,
                    dstart: Int,
                    dend: Int
                ): CharSequence? {
                    val normal = StringBuilder(dest.toString())
                    normal.insert(dstart, source)
                    val num = normal.toString().toInt()
                    if (num > max) {
                        edNum.setText("$max")
                        return ""
                    }
                    if (num < 1) {
                        edNum.setText("1")
                        return ""
                    }
                    return source
                }
            })*/
            edNum.addTextChangedListener(object : SimpleTextWatcher() {
                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                    val msg: String = edNum.text.toString().trim { it <= ' ' }
                    if (TextUtils.isEmpty(msg)) {
                        tvPrice.text = "0"
                        return
                    }
                    val num = msg.toInt()
                    val total = (num * priceSku) / rate
                    tvPrice.text = "$total"

                    edNum.setSelection(msg.length)
                }
            })
        }

        if (!dialog.isShowing) {
            dialog.show()

            edNum.post {
                edNum.requestFocus()
                mInputMethodManager.showSoftInput(edNum, InputMethodManager.SHOW_IMPLICIT)
            }
        }
    }

}